package com.lvxh.exp.bean;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * 
 * 未涉及分页的模块直接继承此类
 * 
 * @author 
 */
public abstract class BaseController extends MultiActionController
{
   
    protected static final Logger LOG = Logger.getLogger(BaseController.class);


    
    public String toString()
    {
        try
        {
            return BeanUtils.describe(this).toString();
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
    }


    protected String getMessage(String key, Object[] values)
    {
        ApplicationContext contxt = WebApplicationContextUtils
                .getWebApplicationContext(getServletContext());
        return contxt.getMessage(key, values, Locale.CHINA);
    }


    protected String getMessage(String key)
    {
        ApplicationContext contxt = WebApplicationContextUtils
                .getWebApplicationContext(getServletContext());
        return contxt.getMessage(key, new Object[] {}, Locale.CHINA);
    }


    /**
     * 取得session对象
     * 
     * @author 
     * @return
     * @throws
     */
    protected HttpSession getSession()
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return request.getSession();
    }


    /**
     * 取得request对象
     * 
     * @author 
     * @return
     * @throws
     */
    protected HttpServletRequest getRequest()
    {
        return ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
    }

    
    protected int procesPage(BaseHolder seq, int count)
    {
        int sum;
        seq.setPerNumber(10);
        if(count/seq.getPage() == 0)
        {
            sum = count/seq.getPerNumber();
        }
        else
        {
            sum = count/seq.getPerNumber()+1;
        }
       
        seq.setStartRecordNum((seq.getPage()-1)*10);
        return sum;
    }

}
